/**
 * @(#)addPatientPanel.java
 *
 *
 * @author 
 * @version 1.00 2010/2/20
 */
package drops.gui;

import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;

import drops.control.PatientControl;
import drops.dao.Patient;
import drops.dao.PatientPeer;

import java.awt.*;
import java.awt.event.*;
import java.text.NumberFormat;
import java.util.*;

public class AddPatientPanel extends JPanel implements ActionListener{

	JLabel patientNumberLabel;
	JLabel firstNameLabel;
	JLabel middleNameLabel;
	JLabel lastNameLabel;
	JLabel addressLabel;
	JLabel contactNumberLabel;
	JLabel ageLabel;	
	JLabel occupationLabel;
	JLabel statusLabel;
	JLabel complainLabel;	
		
	JTextField patientNumberField;
	JTextField firstNameField;
	JTextField middleNameField;
	JTextField lastNameField;
	JTextField addressField;
	JTextField contactNumberField;
	JFormattedTextField ageField;	
	JTextField occupationField;	
	JTextField complainField;

	JComboBox statusField;
	ComboBoxModel statusModel;
	
	JButton submitButton;
	JButton clearButton;
	
	MainFrame mainFrame;
	
	PatientPanel patientPanel;
	
    public AddPatientPanel(MainFrame mainFrame) {
    	this.mainFrame = mainFrame;
    	
    	patientNumberLabel = new JLabel("Patient Number:");
		firstNameLabel = new JLabel("First Name:");
		middleNameLabel = new JLabel("Middle Name:");
		lastNameLabel = new JLabel("Last Name:");
		addressLabel = new JLabel("Address:");
		contactNumberLabel = new JLabel("Contact Number:");
		ageLabel = new JLabel("Age:");
		occupationLabel = new JLabel("Occupation:");
		statusLabel = new JLabel("Status:");
		complainLabel = new JLabel("Complain:");
		
		patientNumberField = new JTextField(50);
		firstNameField = new JTextField(50);
		middleNameField = new JTextField(50);
		lastNameField = new JTextField(50);
		addressField = new JTextField(50);
		contactNumberField = new JTextField(50);
		ageField = new JFormattedTextField(NumberFormat.getIntegerInstance());
		ageField.setValue(0);
		occupationField = new JTextField(50);
		
		//Status dropdown
		String [] statusChoices = {"Single", "Married", "Divorced", "Widowed"};
		statusModel = new DefaultComboBoxModel(statusChoices);
		statusField = new JComboBox(statusModel);
		
		complainField = new JTextField(50);
		
		submitButton = new JButton("Submit");
		submitButton.addActionListener(this);
    	clearButton = new JButton("Clear");
    	clearButton.addActionListener(this);
    	
    	setLayout(new GridLayout(11,2));
    	
    	add(firstNameLabel);
    	add(firstNameField);
    	add(middleNameLabel);
    	add(middleNameField);
    	add(lastNameLabel);
    	add(lastNameField);
    	add(addressLabel);
    	add(addressField);
    	add(contactNumberLabel);
    	add(contactNumberField);
   		add(ageLabel);
    	add(ageField);
    	add(occupationLabel);
    	add(occupationField);
    	add(statusLabel);
    	add(statusField);
    	add(complainLabel);
    	add(complainField);
    	add(submitButton);
    	add(clearButton);
    }

    
    
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource()==submitButton) {
			addPatient();
			
		}
		else if(e.getSource()==clearButton){
			clear();			
		}
	}
    
	private void clear(){
		firstNameField.setText("");
		middleNameField.setText("");
		lastNameField.setText("");
		addressField.setText("");
		contactNumberField.setText("");
		ageField.setValue(0);
		occupationField.setText("");
		statusField.setSelectedIndex(0);
		complainField.setText("");
	}

    private void addPatient() {
    	String patientFirstName = firstNameField.getText();
    	String patientMiddleName = middleNameField.getText();
    	String patientLastName = lastNameField.getText();
    	String address = addressField.getText();
    	String contactNumber = contactNumberField.getText();
    	int age = Integer.parseInt(ageField.getValue().toString());
    	String occupation = occupationField.getText();
    	String status = statusField.getSelectedItem().toString();
    	String complain = complainField.getText();
    	int key;
    	if ((key = PatientControl.addPatient(patientFirstName, patientMiddleName, patientLastName, address, contactNumber, age, occupation, status, complain))>=0) {
    		clear();
    		JOptionPane.showMessageDialog(this, "Add Patient successful");
    		mainFrame.refreshPatients();
    		mainFrame.closeAddPatientFrame();
    	}
    	else {
    		JOptionPane.showMessageDialog(this, "Add Patient failed");
    	}
    }
    
}